<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-button
          size="small"
          @click="$router.push({name:'add-menu'})"
          :disabled="loading">添加菜单</el-button>
      </div>
      <el-table
        :data="menus"
        border
        style="width: 100%"
        v-loading="loading">
        <el-table-column
          label="编号"
          prop="id"
          min-width="150">
        </el-table-column>
        <el-table-column
          prop="name"
          label="菜单名称"
          min-width="150">
        </el-table-column>
        <el-table-column
          label="菜单级数"
          min-width="150">
          <template slot-scope="scope">
            <span>{{handlerLevel(scope.row)}}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="icon"
          label="前端图标"
          min-width="150">
        </el-table-column>
        <el-table-column
          prop="orderNum"
          label="排序"
          min-width="150">
        </el-table-column>
        <el-table-column
          label="操作"
          min-width="150">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="current"
      :page-sizes="[3, 5, 7, 9]"
      :page-size="size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="menus.length">
    </el-pagination> -->
  </div>
</template>

<script lang="ts">
import { DataArray, deleteMenu, getAll } from '@/services/menu'
import Vue from 'vue'
export default Vue.extend({
  name: 'MenuIndex',
  data () {
    return {
      menus: [],
      loading: true,
      size: 5,
      current: 1
    }
  },
  methods: {
    // 获取数据
    async handlerShowMenu () {
      // const { data } = await getMenuNodeList()

      // if (data.code !== '000000') {
      //   return this.$message.error('获取菜单列表失败')
      // }
      // this.handlerData(data.data as DataArray[])
      // console.log('tabled', this.menus)
      this.loading = true
      const { data } = await getAll()
      this.menus = data.data
      this.loading = false
      // console.log('当前数据', this.menus)
    },
    // handlerData (data:DataArray[]) {
    //   for (let i = 0; i < data.length; i++) {
    //     if (!data[i].subMenuList) {
    //       (this.menus as DataArray[]).push(data[i])
    //     } else {
    //       this.handlerData(data[i].subMenuList as DataArray[])
    //     }
    //   }
    // },
    // 编辑
    handleEdit (index:number, row:DataArray) {
      // console.log(index, row)
      this.$router.push({
        name: 'edit-menu',
        params: {
          id: row.id.toString()
        }
      })
    },
    // 删除
    handleDelete (index:number, row:DataArray) {
      // console.log(index, row)

      this.$confirm('此操作将删除一项菜单, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data } = await deleteMenu(row.id)
        // console.log('删除打印', data)

        if (data.code === '000000') {
          this.$message.success('删除成功')
          this.handlerShowMenu()
        }
      }).catch(() => {
        this.$message.error('已取消删除')
      })
    },
    // 处理下等级显示方式
    handlerLevel (row:DataArray) {
      let str = ''
      switch (row.level) {
        case 0:
          str = '一级'
          break
        case 1:
          str = '二级'
          break
        case 2:
          str = '三级'
          break
        default:
          str = ''
          break
      }
      return str
    }
    // handleSizeChange (val:number) {
    //   console.log(`每页 ${val} 条`)
    //   this.size = val
    // },
    // handleCurrentChange (val:number) {
    //   console.log(`当前页: ${val}`)
    //   this.current = val
    // }
  },
  created () {
    this.handlerShowMenu()
  }
})
</script>

<style lang="scss" scoped>

</style>
